package com.sskj.fabi.presenter;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.MD5Util;
import com.sskj.common.util.ToastUtil;
import com.sskj.fabi.bean.BuySellBean;
import com.sskj.fabi.bean.OrderDetailBean;
import com.sskj.fabi.bean.PayWayItem;
import com.sskj.fabi.http.HttpConfig;
import com.sskj.fabi.ui.fragment.BuySellFragment;
import com.sskj.lib.bean.PageBean;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.http.JsonConverter;
import com.sskj.lib.util.CommonUtil;

import java.util.List;

import io.reactivex.Flowable;


public class BuySellFragmentPresenter extends BasePresenter<BuySellFragment> {

    public Flowable<List<BuySellBean>> getData(String page, String pageSize, boolean isBuy) {
        requsetPayWayList();
        return httpService.getSellBuy(page, pageSize, isBuy)
                .converter(new JsonConverter<HttpData<PageBean<BuySellBean>>>() {
                })
                .adapt(new FlowableBody<>())
                .map(pageBeanHttpData -> pageBeanHttpData.getData().getList());
    }

    public void order(String tpwd, String order_no, String total_num, String total_price, boolean isBuy) {
        httpService.order(CommonUtil.getFiveMd5Value(tpwd), order_no, total_num, total_price, isBuy)
                .execute(new JsonCallBack<HttpData<OrderDetailBean>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<OrderDetailBean>> response) {
                        HttpData<OrderDetailBean> httpData = response.body();
                        ToastUtil.showShort(httpData.getMsg());
                        if (httpData.getStatus() == HttpConfig.OK) {

                            mView.orderSuccess(httpData.getData());
                        }
                    }
                });
    }

    // 获取支付方式列表
    public void requsetPayWayList() {
        OkGo.<HttpData<List<PayWayItem>>>post(HttpConfig.BASE_URL + HttpConfig.PAY_WAY_LIST)
                .execute(new JsonCallBack<HttpData<List<PayWayItem>>>(this, false) {
                    @Override
                    public void onSuccess(Response<HttpData<List<PayWayItem>>> response) {
                        mView.updateUi(response.body().getData());
                    }
                });
    }


}
